package Application.MapApplication.view.ui
{	
	import Application.MapApplication.def.Def;
	import def.XG;
	import Application.MapApplication.def.Manager;
	
	import fl.controls.Button;
	import fl.controls.CheckBox;
	import fl.controls.DataGrid;
	import fl.controls.TextInput;
	import fl.controls.dataGridClasses.DataGridColumn;
	
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class CreateMapMed extends UIBaseMed
	{
		public var view : Sprite;
		
		private var _closeBtn : SimpleButton;
		private var _sureBtn : Button;
		private var _dragCkb : CheckBox;
		
		private var _panTitle : Sprite;
		
		private var _tileSizeTxt : TextInput;
		private var _colNumTxt : TextInput;
		private var _rowNumTxt : TextInput;
		private var _bkImgTxt : TextInput;
		private var _txtMapName : TextInput;
		private var _txtMapType : TextInput;
		
		public function CreateMapMed()
		{	
			super(Def.CreateMap,[Def.STAGE_ALIGN_MIDDLE]);
	
			view = this.viewComponent as Sprite;
		}
		
		override public function onRegister():void
		{
			_closeBtn = 			view["Btn_Close"];
			_sureBtn =				view["Btn_Sure"];
			_dragCkb = 				view["Ckb_Drag"];
			
			_panTitle = 			view["Pan_Title"];
			
			_tileSizeTxt = 			view["TitleSizeTxt"];
			_colNumTxt =			view["ColumnTxt"];
			_rowNumTxt = 			view["RowTxt"];
			_bkImgTxt = 			view["BKMapTxt"];
			_txtMapName = 			view["TxtMapName"];
			_txtMapType = 			view["TxtMapType"];
			
			_closeBtn.addEventListener(MouseEvent.CLICK,OnClose);
			_panTitle.addEventListener(MouseEvent.MOUSE_DOWN,OnPanTitleMDown);
			_panTitle.addEventListener(MouseEvent.MOUSE_UP,OnPanTitleMUp);
			
			_sureBtn.addEventListener(MouseEvent.CLICK,OnCreateMap);
			_dragCkb.addEventListener(Event.CHANGE,OnDragFlag);
			
			view.addEventListener(MouseEvent.MOUSE_DOWN,OnMDown);
		}
		
		private function OnMDown(evt : MouseEvent) : void
		{
			XG.swapUI(this.getMediatorName());
		}
		
		private function OnPanTitleMDown(evt : MouseEvent) : void
		{
			view.alpha = 0.5;
			view.startDrag();
		}
		
		private function OnPanTitleMUp(evt : MouseEvent) : void
		{
			view.alpha = 1;
			view.stopDrag();
		}
		
		private function OnCreateMap(evt : MouseEvent) : void
		{
			var content : Object = new Object;
			content["colNum"] = int(_colNumTxt.text);
			content["rowNum"] = int(_rowNumTxt.text);
			content["tileSize"] = int(_tileSizeTxt.text);
			content["mapName"] = _txtMapName.text;
			content["bkMapName"] = _bkImgTxt.text;
			content["mapType"] = _txtMapType.text;
			
			sendNotification(Def.CreateMap_CreateGride,content);
		}
		
		private function OnDragFlag(evt : Event) : void
		{
			sendNotification(Def.MapEdit_DragMap,_dragCkb.selected);
		}
		
		private function OnClose(evt : MouseEvent) : void
		{
			sendNotification(Def.CreateMap_Close);
		}
		
		override public function listNotificationInterests() : Array
		{
			var notificatons : Array = super.listNotificationInterests();
			
			notificatons=notificatons.concat([Def.CreateMap_Show,
											  Def.CreateMap_Close,
											  Def.CreateMap_Turn,
											  Def.STAGE_SIZE_CHANGED
											  ]);
			
			return notificatons;
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case Def.CreateMap_Show:
					XG.openUI(this.mediatorName);
					break;
				case Def.CreateMap_Close:
					destory();
					XG.closeUI(this.mediatorName);
					break;
				case Def.CreateMap_Turn : 
					destory();
					XG.turnUI(this.mediatorName);
					break;
				default :
					break;
			}
			
			super.handleNotification(notification);
		}
		
		private function destory() : void
		{
			if(!XG.hasUI(this.getMediatorName())) return;
			
			facade.removeMediator(Def.CreateMap);
			facade.removeProxy(Def.CreateMap);
			
			_closeBtn.removeEventListener(MouseEvent.CLICK,OnClose);
			_panTitle.removeEventListener(MouseEvent.MOUSE_DOWN,OnPanTitleMDown);
			_panTitle.removeEventListener(MouseEvent.MOUSE_UP,OnPanTitleMUp);
			
			_sureBtn.removeEventListener(MouseEvent.CLICK,OnCreateMap);
			_dragCkb.removeEventListener(Event.CHANGE,OnDragFlag);
			
			view.removeEventListener(MouseEvent.MOUSE_DOWN,OnMDown);
			
			view = null;
			_closeBtn = null;
			_panTitle = null; 
			_sureBtn = null;
			_dragCkb = null;
			
			_tileSizeTxt = null;
			_colNumTxt = null;
			_rowNumTxt = null;
			_bkImgTxt = null;
		}
		
	}
}